package vendingmachine

class FakeCSController {

	private def venderItems = [
		'cow' ,
		'checken' ,
		'tiger' ,
		'dragon',
		'frank' ,
		'beef' ,
		'lamb' ,
		'lion' ,
		'bug'
	]
	private def cards = [
		'0000000000' ,
		'111' ,
		'2222222222' ,
		'3333333333',
		'4444444444' ,
		'5555555555',
		'6666666666',
		'7777777777',
		'8888888888',
		'9999999999'
	]
	//    def index() {
	////		println params
	//		if(params.startedTime!=null && params.endTime!=null){
	//			int offset = Long.parseLong(params.endTime) - Long.parseLong(params.startedTime)
	//			def result = [:]
	//			def random = new Random()
	//			(0..1).each {
	//				def deviceId= random.nextInt()+""
	//				result."${deviceId}" = []
	//				(0..2).each {
	//					def tmpRecord = [
	//									 cardId:cards[random.nextInt(cards.size())],
	//									 tradedItem:venderItems[random.nextInt(venderItems.size())],
	//									 tradedTime:(Long.parseLong(params.startedTime) + random.nextInt(offset))
	//									 ]
	//					result."${deviceId}".add(tmpRecord)
	//				}
	//			}
	//
	//			render(contentType:"application/json") {
	//				result
	//			}
	//		}
	//	}

	def index() {
		if(params.startedTime!=null && params.endTime!=null){
			long et = Long.parseLong(params.endTime)
			long st = Long.parseLong(params.startedTime)
			def result = [:]
			def r = CsData.createCriteria().list {
				between("dateCreated",new Date(st),new Date(et))
			}
			result."11" = []
			r.each {
				result."11".add([cardId:it.cid, tradedItem:it.item ,  tradedTime:it.dateCreated.getTime()])
			}
			render(contentType:"application/json") { result }
		}
	}
}
